import { post } from '../../../utils/request.js'
import global from '../../../utils/global.js'
let that
Page({
  data: {
    navData: [{ name: "待支付", count: 0 }, { name: "待体检", count: 0 }, { name: "已体检", count: 0 }],
    list:[],
    counts:[],
    currentTab:0,
    isList:true,
    userId: wx.getStorageSync("useInfo").id,
    height: '',
    page:1,
    isMore:[true,true,true],
    prev:false
  },


/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
that = this
  wx.getSystemInfo({
    success: (res) => {
      that.setData({
        height: res.windowHeight - 50
      })
    }
  })
  
  wx.removeStorageSync("selectCurrentDate");
  wx.removeStorageSync("orderProductId")
},
  onShow: function () {
    that.setData({
      page: 1,
      list: []
    })
    that.orderListCount() 
    that.listData()
    console.log(that.data.prev)
    if (that.data.prev){   
      that.updateFun(wx.getStorageSync("orderProductId"),wx.getStorageSync("selectCurrentDate"))
    }
  },
// 订单数量orderListCount
orderListCount(){
  let param = {
    userId:that.data.userId
    // userId: "1738"
  }
  post(global.apiUrl.orderListCount, param, function (res) {
    that.data.counts[0] = res.obj.waitPayCount 
    that.data.counts[1] = res.obj.waitAppoinmentConfirmedCount  
    that.data.counts[2] = res.obj.waitCommentCount  
    that.setData({
      counts: that.data.counts
    })
  })
},
 
// 点击切换
currentList(e){
console.log(e.currentTarget.dataset.index)
  that.setData({
    currentTab: e.currentTarget.dataset.index,
    list:[],
    page:1
  })
  that.listData()
},
listData(){
  // console.log(that.data.isMore[that.data.currentTab]) 
  // 获取列表
  let param = {
    page:that.data.page++,
    dataType: that.data.currentTab,
    userId:that.data.userId
    // userId:"1738"
  }
  post(global.apiUrl.orderlist, param, function (res) {
    if (!res.more || res.obj.length == 0) {
      wx.showToast({
        title: '已经加载完数据',
        icon: 'loading',
        duration: 1000
      })
      that.data.isMore[that.data.currentTab] = false
      that.setData({
        isMore : that.data.isMore
      })
      return
    }
    let curArr = that.data.list.concat(res.obj)
    that.setData({
      list: curArr
    })
   
  })
},
// 去订单详情
goListDetail(e){
  console.log(e)
  wx.navigateTo({
    url: 'orderDetail/orderDetail?orderId=' + e.currentTarget.dataset.id + '&orderDetailType=' + e.currentTarget.dataset.orderdetailtype + '&nopay=' + e.currentTarget.dataset.nopay,
  })
},
  onPullDownRefresh: function () {
    // 停止下拉动作
    wx.stopPullDownRefresh();

  },

  lower: function () {
    if (!that.data.isMore[that.data.currentTab]) {
      return
    }
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    // 页数+1
    that.listData()
  },
  // 取消订单
  cancelOrder(e){
    console.log(e.currentTarget.dataset.id)
    let param = {
      orderId: e.currentTarget.dataset.id,
      userId:that.data.userId
      // userId: "1738"
    }
    wx.showModal({
      title: '提示',
      content: '您确定要取消订单吗？',
      success:function(res){
        if (res.confirm){
          post(global.apiUrl.cancelOrder, param, function (res) {     
            if (res.msg =="取消成功"){
              that.onShow()
            }
          })
        }
      }
    })
    
  },
  // 改期
  updateOrderAppointment(e){
    console.log(e.currentTarget.dataset.orderproductid)
    wx.setStorageSync("orderProductId", e.currentTarget.dataset.orderproductid)
    wx.navigateTo({
      url: '/pages/calendar/calendar?productId=' + e.currentTarget.dataset.id,
    })
  },
  updateFun(orderProductId,appointmentDate){
    // 1.orderProductId 2.appointmentDate,3.userId
    let param ={
      orderProductId: orderProductId,
      appointmentDate,
      userId:that.data.userId
    }
    console.log(param)
    post(global.apiUrl.updateOrderAppointment, param, function (res) {
      console.log(res)
      wx.showToast({
        title: '改期'+res.msg,
        icon: 'succes',
        duration: 1000,
        mask: true
      })
    })
  },
  // 支付
  wechatAppletPay(e){
    let order = e.currentTarget.dataset.order
    let code = "";
    let payId = e.currentTarget.dataset.id
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求  
          console.log(res.code)
          code = res.code;
          console.log(code,"zaoqij")
          that.isPayInfo(code,payId,order)
        } else {
          wx.showModal({
            title: '提示',
            content: '获取用户信息失败！',
          })
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  },
  isPayInfo(code, payId,obj){
    console.log(obj)
    let param = {
      code: code,
      payId: payId
    }
    post(global.apiUrl.wechatAppletPay, param, function (res) {
      console.log(res)
      that.wxPayOrder(res.obj, obj, payId)
    })
  },
  // 支付接口
  wxPayOrder: function (result, obj, payId) { //去微信支付-小程序
    let resultVal = result
    wx.requestPayment({
      "appId": resultVal.appId,
      "nonceStr": resultVal.nonceStr,
      "signType": resultVal.signType,
      "package": resultVal.pkg,
      "timeStamp": resultVal.timeStamp + '',//数值转化为字符串
      "paySign": resultVal.paySign,
      'success': function (res) {
        wx.hideLoading()
        wx.reLaunch({
          url: "/pages/pay/paySuccess/paySuccess?pay=1&order=" + JSON.stringify(obj) + "&payId=" + payId
        })

      },
      'fail': function (res) {
        console.log(res)
        wx.showModal({
          title: '提示',
          content: '支付失败',
          success: function () {
            wx.reLaunch({
              url: "/pages/pay/paySuccess/paySuccess?pay=0&order=" + JSON.stringify(obj) + "&payId=" + payId
            })
          },//接口调用成功
          fail: function () {
            wx.reLaunch({
              url: "/pages/pay/paySuccess/paySuccess?pay=0&order=" + JSON.stringify(obj) + "&payId=" + payId
            })
          },  //接口调用失败的回调函数  
        })


      },
      'complete': function (res) { }
    })
  },

})
